package com.winter.controller;


import com.winter.model.News;
import com.winter.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RequestMapping("/news")
@Controller
public class NewsController {
    @Autowired 
    private NewsService newsService;
    //定时的添加
    @ResponseBody
    @RequestMapping("/insert")
    public String insertNews(News news){
        news.setId(UUID.randomUUID().toString().replace("-",""));
        news.setStatus("1");
        news.setTime(new Date());
        newsService.insertNews(news);
        return "redirect:/news/query/1/10";
    }
    //删除
    @ResponseBody
    @RequestMapping("/delete")
    public void deleteNews(News news){
        String status = news.getStatus();
        int i = Integer.parseInt(status)^3;
        news.setStatus(String.valueOf(i));
        newsService.uqdateNews(news);
    }  
    //查询单个

    // 查询全部
    @RequestMapping("/query/{pageNum}/{pageSize}")
    public String queryAll(Model model, @PathVariable("pageNum")int pageNum,@PathVariable("pageSize") int pageSize){
        List<News> news = newsService.queryAll(pageNum,pageSize);
        model.addAttribute("news", news);
        return "article-list";
    }
    // 类别查询
    @RequestMapping("/queryCategory")
    public String queryCategory(String categoryId, Model model) throws Exception{
        List<News> news = newsService.queryCategory(categoryId);
        System.out.println(news);
        model.addAttribute("news", news);
        return "article-list";
    }
    //查询一个
    @RequestMapping("/queryOne")
    public void queryOne(String id,Model model){
        System.out.println(id);
        News news = newsService.queryOne(id);
        String status = news.getStatus();
        if(status.equals("1")){
            news.setStatus(String.valueOf(0));
        }else news.setStatus(String.valueOf(1));
        newsService.uqdateNews(news);
    }
}
